package com.kaishengit.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.kaishengit.dao.MemberDao;
import com.kaishengit.dao.ProjectDao;
import com.kaishengit.dao.ProjectMemberDao;
import com.kaishengit.entity.Member;
import com.kaishengit.entity.Project;
import com.kaishengit.entity.ProjectMember;
import com.kaishengit.entity.Task;

public class ProjectService {
	ProjectDao dao = new ProjectDao();
	public void insert(String name,String dec,String members,String manager){
		Project p = new Project();
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String time = sdf.format(new Date());
		SimpleDateFormat s = new SimpleDateFormat("mmss");
		String id = s.format(new Date());
		p.setP_id(id);
		p.setP_name(name);
		p.setP_dec(dec);
		p.setP_createTime(time);
		p.setP_members(members);
		p.setP_manager(manager);
		dao.insert(p);
		String[] array = members.split(",");
		MemberDao mdao = new MemberDao();
		for(int i = 0;i< array.length;i++){
			String m = array[i];
			Member member = mdao.findByName(m);
			if(member != null){
				ProjectMemberDao pmdao = new ProjectMemberDao();
				ProjectMember pm = new ProjectMember();
				pm.setPid(id);
				pm.setMid(member.getM_id());
				pm.setRole("member");
				pmdao.insert(pm);
			}
		}
	}
	public Project findById(String pid) {
		return dao.findById(pid);
		
	}
	public Project findByName(String name) {
		return dao.findByName(name);
	}
	public Member findByMemberName(String m) {
		MemberDao mdao = new MemberDao();
		return mdao.findByName(m);
	}
	
	
	

}
